package sg.edu.nus.iss.phoenix.schedule.service;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
/**
 * 
 * @author Goh Zhe Yi, Ramprasad, Karthic Kumar
 *
 */

public class DateUtil {
	
	public static final String[] yearList = {	"2012", 
												"2013",
												"2014"};
	
	public static HashMap<Integer, String> getDays(){
		HashMap<Integer, String> weekDays = new HashMap<Integer, String>();
		weekDays.put(Integer.valueOf(1), "Sunday");
		weekDays.put(Integer.valueOf(2), "Monday");
		weekDays.put(Integer.valueOf(3), "Tuesday");
		weekDays.put(Integer.valueOf(4), "Wednesday");
		weekDays.put(Integer.valueOf(5), "Thursday");
		weekDays.put(Integer.valueOf(6), "Friday");
		weekDays.put(Integer.valueOf(7), "Saturday");
		return weekDays;
		}
	
	public static HashMap<Integer, String> getAllTimings(){
		HashMap<Integer, String> allTimings = new HashMap<Integer, String>();
		allTimings.put(Integer.valueOf(1), "00:00 - 00:30");
		allTimings.put(Integer.valueOf(2), "00:30 - 01:00");
		allTimings.put(Integer.valueOf(3), "01:00 - 01:30");
		allTimings.put(Integer.valueOf(4), "01:30 - 02:00");
		allTimings.put(Integer.valueOf(5), "02:00 - 02:30");
		allTimings.put(Integer.valueOf(6), "02:30 - 03:00");
		allTimings.put(Integer.valueOf(7), "03:00 - 03:30");
		allTimings.put(Integer.valueOf(8), "03:30 - 04:00");
		allTimings.put(Integer.valueOf(9), "04:00 - 04:30");
		allTimings.put(Integer.valueOf(10), "04:30 - 05:00");
		allTimings.put(Integer.valueOf(11), "05:00 - 05:30");
		allTimings.put(Integer.valueOf(12), "05:30 - 06:00");
		allTimings.put(Integer.valueOf(13), "06:00 - 06:30");
		allTimings.put(Integer.valueOf(14), "06:30 - 07:00");
		allTimings.put(Integer.valueOf(15), "07:00 - 07:30");
		allTimings.put(Integer.valueOf(16), "07:30 - 08:00");
		allTimings.put(Integer.valueOf(17), "08:00 - 08:30");
		allTimings.put(Integer.valueOf(18), "08:30 - 09:00");
		allTimings.put(Integer.valueOf(19), "09:00 - 09:30");
		allTimings.put(Integer.valueOf(20), "09:30 - 10:00");
		allTimings.put(Integer.valueOf(21), "10:00 - 10:30");
		allTimings.put(Integer.valueOf(22), "10:30 - 11:00");
		allTimings.put(Integer.valueOf(23), "11:00 - 11:30");
		allTimings.put(Integer.valueOf(24), "11:30 - 12:00");
		allTimings.put(Integer.valueOf(25), "12:00 - 12:30");
		allTimings.put(Integer.valueOf(26), "12:30 - 13:00");
		allTimings.put(Integer.valueOf(27), "13:00 - 13:30");
		allTimings.put(Integer.valueOf(28), "13:30 - 14:00");
		allTimings.put(Integer.valueOf(29), "14:00 - 14:30");
		allTimings.put(Integer.valueOf(30), "14:30 - 15:00");
		allTimings.put(Integer.valueOf(31), "15:00 - 15:30");
		allTimings.put(Integer.valueOf(32), "15:30 - 16:00");
		allTimings.put(Integer.valueOf(33), "16:00 - 16:30");
		allTimings.put(Integer.valueOf(34), "16:30 - 17:00");
		allTimings.put(Integer.valueOf(35), "17:00 - 17:30");
		allTimings.put(Integer.valueOf(36), "17:30 - 18:00");
		allTimings.put(Integer.valueOf(37), "18:00 - 18:30");
		allTimings.put(Integer.valueOf(38), "18:30 - 19:00");
		allTimings.put(Integer.valueOf(39), "19:00 - 19:30");
		allTimings.put(Integer.valueOf(40), "19:30 - 20:00");
		allTimings.put(Integer.valueOf(41), "20:00 - 20:30");
		allTimings.put(Integer.valueOf(42), "20:30 - 21:00");
		allTimings.put(Integer.valueOf(43), "21:00 - 21:30");
		allTimings.put(Integer.valueOf(44), "21:30 - 22:00");
		allTimings.put(Integer.valueOf(45), "22:00 - 22:30");
		allTimings.put(Integer.valueOf(46), "22:30 - 23:00");
		allTimings.put(Integer.valueOf(47), "23:00 - 23:30");
		allTimings.put(Integer.valueOf(48), "23:30 - 24:00");
		return allTimings;		
	}
	
	/**
	 * This get method gets the Current date in java.sql.date format
	 * @return java.sql.date
	 */
	public static Date getCurrSQLDate(){
		return new Date((Calendar.getInstance().getTime()).getTime());
	}
	
	public static ArrayList<Date> getDatesOfCurrentWeek(){
		Calendar cal = Calendar.getInstance();
		
		for(int i = 0; i<=6; i++){
			
		}
		
		return null;
		
	}
	
	/**
	 * Converts a String date into java.sql.date format
	 * @param dateString Date in String type
	 * @return java.sql.date object based on String date
	 */
	public static Date convertToSQLDate(String dateString){
		DateFormat df = DateFormat.getDateInstance();
		try {
			return new Date(df.parse(dateString).getTime());
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * Converts a String date and time into java.sql.date format
	 * @param dateString Date and Time in String type
	 * @return java.sql.date object based on String date and time
	 */
	public static Date convertToSQLTime(String dateString){
		DateFormat df = DateFormat.getDateInstance();
		try {
			return new Date(df.parse(dateString).getTime());
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * This method adds the days to a date
	 * @param date Date object to add the days to
	 * @param amount integer of the amount of days to add
	 * @return new Date object with the added days to the input date
	 */
	public static Date addDays(Date date, int amount){
		Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, amount);

        return new Date(cal.getTime().getTime());
	}
	
	/**
	 * Gets the current date in java.sql.date format
	 * @return java.sql.date
	 */
	public static Date getCurrentDate(){
		Calendar cal = Calendar.getInstance();

        return new Date(cal.getTime().getTime());
	}
	
	
	/**
	 * Getting the start date of the week from any date within that week
	 * @param Date within the week 
	 * @return Date object which is the start date of the week for the input date
	 */
	public static Date getFirstDateOfTheWeek(Date date) {
		Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, 1-cal.get(Calendar.DAY_OF_WEEK));
		
		return new Date(cal.getTime().getTime());
	}
	
	/**
	 * Converts the date String into a date format for MySQL
	 * @param oldFormatDate String of the date to be converted
	 * @return String of the formatted date
	 * @throws Exception
	 */
	public static String convertToMySQLDateFormat(String oldFormatDate) throws Exception{
		SimpleDateFormat sdfAct = new SimpleDateFormat("dd-MM-yyyy");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date temp = sdfAct.parse(oldFormatDate);
		return sdf.format(temp);
	}
	
	/**
	 * Converts the date String into a date format for Java
	 * @param oldFormatDate String of the date to be converted
	 * @return String of the formatted date
	 * @throws Exception
	 */
	public static String convertToJavaDateFormat(String oldFormatDate) throws Exception{
		SimpleDateFormat sdfAct = new SimpleDateFormat("dd-MM-yyyy");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date temp = sdf.parse(oldFormatDate);
		return sdfAct.format(temp);
	}
	
	/**
	 * Converts the date String into a date format for the system
	 * @param oldFormatDate String of the date to be converted
	 * @return String of the formatted date
	 * @throws Exception
	 */
	public static String convertProgramDateFormat(String oldFormatDate) throws Exception{
		SimpleDateFormat sdfAct = new SimpleDateFormat("dd-MM-yyyy");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(sdfAct.parse(oldFormatDate));
	}
	
	/**
	 * Converts the date String into a date format for MySQL
	 * @param oldFormatDate String of the date to be converted
	 * @return String of the formatted date
	 * @throws Exception
	 */
	public static String convertToMySQLDateFormat1(String oldFormatDate) throws Exception{
		SimpleDateFormat sdfAct = new SimpleDateFormat("dd-MM-yyyy");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date temp = sdf.parse(oldFormatDate);
		return sdfAct.format(temp);
	}
	
	/**
	 * Converts the date and time String into a date and time format for MySQL
	 * @param oldFormatDate String of the date and time to be converted
	 * @return String of the formatted date and time
	 * @throws Exception
	 */
	public static String convertToMySQLTimeFormat(String oldFormatDate) throws Exception{
		SimpleDateFormat sdfAct = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		java.util.Date temp = sdfAct.parse(oldFormatDate);
		return sdf.format(temp);
	}
	
	/**
	 * Converts the start date and time String into a date and time format
	 * @param oldFormatDate String of the date and time to be converted
	 * @return String of the formatted date and time
	 * @throws Exception
	 */
	public static String convertToStartTimeFormat(String oldFormatDate) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		return sdf.format(sdf.parse(oldFormatDate));
	}
	
	public static java.util.Date convertStringTodate(String dateValue) throws Exception
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		return sdf.parse(dateValue);
		
	}

	/**
	 * This method gets the time of in partitions of 30 minutes
	 * @param oldFormatDate the Date and Time in String format
	 * @return Long object which contains the time in partitions of 30mins
	 * @throws Exception
	 */
	public static long getTimeInHours(String oldFormatDate) throws Exception{
		SimpleDateFormat sdfAct = new SimpleDateFormat("HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(sdfAct.parse("00:30:00"));
		cal.setTime(sdfAct.parse(oldFormatDate));
		
		@SuppressWarnings("deprecation")
		long hrs = (cal.getTime().getHours() * 2) + (cal.getTime().getMinutes() / 30);
		return hrs;
	}
}
